package com.dao.impl;

import com.dao.UserDao;
import com.pojo.User;
import com.uilt.JdbcUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public ResultSet getUserOne(User user) {
        String sql="select * from dy_user  where uname=? and upassword=?";
        List<Object> list = new ArrayList<Object>();
        list.add(user.getName());
        list.add(user.getPassword());
        ResultSet object = JdbcUtil.getObject(list, sql);
        return object;
    }

    @Override
    public ResultSet getUserAll() {
        String sql="select * from dy_user ";
        ResultSet object = JdbcUtil.getObject( new ArrayList<Object>(), sql);
        return object;
    }

    @Override
    public ResultSet getUserAll(int page, int limit) {
        String sql="select * from dy_user limit ?,?";
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(page);
        objects.add(limit);
        ResultSet object = JdbcUtil.getObject(objects, sql);
        return object;
    }

    @Override
    public ResultSet getUserOne(int id) {
        String sql="select * from dy_user where id=? ";
        List<Object> list = new ArrayList<Object>();
        list.add(id);
        ResultSet object = JdbcUtil.getObject(list, sql);
        return object;
    }

    @Override
    public int delUser(int id) {
        String sql="delete  from dy_user where id=? ";
        List<Object> list = new ArrayList<Object>();
        list.add(id);
        int i = JdbcUtil.updateObject(list, sql);
        return i ;
    }

    @Override
    public int addUser(User user) {
        String sql="insert into   dy_user(uname,upassword) value(?,?)";
        List<Object> list = new ArrayList<Object>();
        list.add(user.getName());
        list.add(user.getPassword());
        int i = JdbcUtil.updateObject(list, sql);
        return i ;
    }
    @Override
    public int editUser(User user) {
        String sql="update dy_user set uname=?,upassword=? where id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(user.getName());
        list.add(user.getPassword());
        list.add(user.getId());
        int i = JdbcUtil.updateObject(list, sql);
        return i ;
    }
}
